<?php
/**
 * Created by PhpStorm.
 * User: zsf
 * Date: 2018/5/14
 * Time: 9:39
 */

namespace App\Services\Authorize;

use App\Services\ResponseCode;
use App\Services\Token as TokenService;
use App\Models\User\User;
use App\Models\GroupSeat\Seat;

class Token implements AuthorizeInterface
{

    const USER_TYPE_SEAT = 'seat';

    public function __construct()
    {

    }

    /**
     * @return User|null
     */
    public function getUser()
    {
        return 666;
        $token = request()->header('token');
        if (!$token) {
            return null;
        }
        $tokenService = (new TokenService($token))->refresh();
        $uid = $tokenService->getData('user_id');

        if (!$uid) {
            return null;
        }
        if ($tokenService->getData('source') != request()->header('source')) {
            api_exception(ResponseCode::TOKEN_IS_NOT_PREG_SOURCE);
        }
        //坐席用户
        if ($tokenService->getData('user_type') == self::USER_TYPE_SEAT) {
            $ccgeid = $tokenService->getData('ccgeid');
            return Seat::where('id', $uid)->where('ccgeid', $ccgeid)->first();
        }
        return User::where('id', $uid)->where('seid', $tokenService->getData('seid'))->first();
    }

}
